'use strict';

const Service = require('egg').Service;

class UserService extends Service {
    //登录
    async login(username,hmcPwd){
        return await this.app.mysql.query('select * from userlist where username=? and password=?',[username,hmcPwd])
    }
    //查询菜单
    async list(role_id){
        return await this.app.mysql.query(`select * from reslist where power like '%${role_id}%'`)
    }
    
    //修改
    async update(data,id){
        //{data:{username,password},id:2}
        //update userlist set username=?,password=?,role_id=? where id=? 
        let sql = 'update userlist set ';

        let keys = Object.keys(data); //[username,password]
        let arr = Object.values(data); //修改的值
        arr.push(id);

        keys.forEach((item,index) => {
            if(index === keys.length-1){
                //是最后一个条件
                sql += `${item}=?`
            }else{
                //不是最后一个条件
                sql += `${item}=?,`
            }
             //username=?,password=?
        })

        sql += ' where id=?';
        console.log(sql,arr);
        await this.app.mysql.query(sql,arr)
    }

    //查询
    async selectPwd(id,hmcPwd){
        return await this.app.mysql.query('select * from userlist where id=? and password=?',[id,hmcPwd])
    }

    //修改密码
    async editPwd(id,pwd){
        await this.app.mysql.query('update userlist set password=? where id=?',[pwd,id])
    }
}

//update set username=?,password=? where id=?
module.exports = UserService;


/**
 * select * from list where username=? and classRoom=? and classNum=?  ,[]
 * 
 * 
 * 
 * 
*/